package com.atguigu.gmall.cart.service.impl;

import com.atguigu.gmall.cart.mapper.CartInfoMapper;
import com.atguigu.gmall.cart.service.CartAsyncService;
import com.atguigu.gmall.cart.service.CartService;
import com.atguigu.gmall.model.cart.CartInfo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

/**
 * @author Sanmier
 * @create 2020-12-16 0:24
 */
@Service
public class CartAsyncServiceImpl implements CartAsyncService {


    @Autowired
    private CartInfoMapper cartInfoMapper;


    // 异步修改购物车
    @Override
    @Async
    public void updateCartInfo(CartInfo cartInfo) {
        System.out.println("update --- cartInfo");

        QueryWrapper<CartInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", cartInfo.getUserId());
        queryWrapper.eq("sku_id", cartInfo.getSkuId());
        //cartInfo第一个参数相当于本体  第二个参数相当于更新条件
        cartInfoMapper.update(cartInfo,queryWrapper);

        //cartInfoMapper.updateById(cartInfo);
    }


    // 异步保存购物车
    @Override
    @Async
    public void saveCartInfo(CartInfo cartInfo) {
        System.out.println("insert --- cartInfo");
        cartInfoMapper.insert(cartInfo);
    }


    // 异步删除购物车
    @Override
    @Async
    public void deleteCartInfo(CartInfo cartInfo) {
        //删除数据库+缓存
        QueryWrapper<CartInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", cartInfo.getUserId());
        cartInfoMapper.delete(queryWrapper);

    }


    // 异步更新选中状态
    @Override
    @Async
    public void checkCart(String userId, Integer isChecked, Long skuId) {
        CartInfo cartInfo = new CartInfo();
        cartInfo.setIsChecked(isChecked);

        //更新条件
        QueryWrapper<CartInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", userId);
        queryWrapper.eq("sku_id", skuId);
        cartInfoMapper.update(cartInfo,queryWrapper );
    }


    // 异步查看是谁删除了哪些商品
    @Override
    @Async
    public void deleteCartInfo(String userId, Long skuId) {
        cartInfoMapper.delete(new QueryWrapper<CartInfo>().eq("user_id", userId).eq("sku_id", skuId));

    }
}
